/*
 * Filename    : Entity_Rahwana.java
 * Projectname : RamaShinta
 * Date        : Nov 29, 2012 , 4:13:25 PM 
 * 
 */

package com.amikomgamedev.ramashinta.entity;

import java.util.ArrayList;

import org.andengine.audio.sound.SoundManager;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.sprite.Sprite;
import org.andengine.ui.activity.BaseGameActivity;
import org.andengine.ui.activity.SimpleBaseGameActivity;

import com.amikomgamedev.ramashinta.ButtonShootCooldown;
import com.amikomgamedev.ramashinta.ButtonShootCooldownRahwana;
import com.amikomgamedev.ramashinta.Utils;
import com.amikomgamedev.ramashinta.game.Game_Data;
import com.amikomgamedev.ramashinta.graphic.Graphic_Anim;
import com.amikomgamedev.ramashinta.graphic.Graphic_Detach;
import com.amikomgamedev.ramashinta.sound.Sound_Load;
import com.amikomgamedev.ramashinta.sound.Sound_Manager;
import com.amikomgamedev.ramashinta.state.State_Game_Play;

public class Entity_Rahwana implements Graphic_Anim, Game_Data
{

	public static AnimatedSprite	m_Mc;
	public static AnimatedSprite	c_Mc;
	private Entity_Rama		m_Rama;
	
	private Graphic_Detach mGraphic;
	
	private Sound_Manager mSound;
	
	private SimpleBaseGameActivity m_Activity;
	
	public static int state;
	
	//avoid
	public static int stateMode=0;
	public static int avoidMode=1;
	public static int normalMode=0;
	
	private Entity_Panah panah;
	
	public Bar barRahwana;
	
	public static int lastPoin, poin;
	
	private int getSprLove=0;
	private int getSprChoco=1;
	public static boolean colItemChoco = false;

	public static boolean timerArmorRahwana = true;
	public static boolean armorAktifRahwana = false;
	
	private ArrayList<Entity_Panah>	listPanah	= new ArrayList<Entity_Panah>();
	private boolean isShoot;

	public Entity_Rahwana(SimpleBaseGameActivity activity, AnimatedSprite p_Mc, AnimatedSprite p2_Mc, Entity_Rama p_Rama, Graphic_Detach p_Graphic, Sound_Manager sound, boolean pIsShoot)
	{
		poin = 0;
		lastPoin = 0;
		isShoot = pIsShoot;
		m_Mc = p_Mc;
		c_Mc = p2_Mc;
		m_Rama = p_Rama;
		mGraphic = p_Graphic;
		mSound = sound;
		m_Activity = activity;
		switch_State(MC_UP);
		state = -1;
		sedangMengejarCinta = false;
		panah = new Entity_Panah(m_Activity, Utils.getRatioW(-100), m_Mc.getY(), mGraphic, m_Mc, false, Entity_Panah.PANAHNYA_RAHWANA, sound);
		barRahwana = new Bar(m_Activity, 720, 0, mGraphic, 0);
	}

	public void switch_State(int p_State)
	{
		state = p_State;
		switch (p_State)
		{
			case MC_IDLE:
				m_Mc.animate(MC_RAHWANA_DUR[0], MC_RAHWANA_FRAME[0], true);
				break;
			case MC_UP:
				m_Mc.animate(MC_RAHWANA_DUR[1], MC_RAHWANA_FRAME[1], 1);
				break;
			case MC_DOWN:
				m_Mc.animate(MC_RAHWANA_DUR[2], MC_RAHWANA_FRAME[2], 1);
				break;
			case MC_ATTACK:
				m_Mc.animate(MC_RAHWANA_DUR[3], MC_RAHWANA_FRAME[3], 1);
				break;
			case MC_DOWN_AVOID:
				m_Mc.animate(MC_RAHWANA_DUR[2], MC_RAHWANA_FRAME[2], 1);
				break;
			case MC_CELEBRASI2:
				c_Mc.animate(MC_RAHWANA_DUR[0], MC_RAHWANA_FRAME[0], true);
				break;

		}
	}

	public void onUpdate(float pSecondElapsed, Entity_Shinta shinta, float second, Entity_Rama rama)
	{
		switch(state)
		{
			case MC_UP:
			{
				if(!checkCollition(MC_UP))
				{
					m_Mc.setY(m_Mc.getY() - Utils.getRatioH(4));
				}
			}
			break;
			case MC_DOWN:
			{
				if(!checkCollition(MC_DOWN))
				{
					if(stateMode==avoidMode){
						m_Mc.setY(m_Mc.getY() + Utils.getRatioH(150));
					}else if(stateMode==normalMode){
					m_Mc.setY(m_Mc.getY() + Utils.getRatioH(4));
					}
				}
			}
			break;
		}
		
		UpdateAI(shinta,second);
		UpdatePanah(second, shinta, Entity_Panah.PANAHNYA_RAHWANA);
		UpdatePendapatan();
	}
	
	void UpdatePendapatan()
	{
		if(lastPoin != poin)
		{
			int perubahan = poin - lastPoin;
			lastPoin = poin;
			Utils.TRACE(""+perubahan);
			barRahwana.spr[1].setY(barRahwana.spr[1].getY()-(perubahan*Utils.getRatioH(30)));
			
			if(barRahwana.spr[1].getY() > 0)
			{
				barRahwana.spr[1].setY(0);
			}
		}
		
	}
	
	private boolean checkCollition(int state)
	{

		if (m_Mc.collidesWith(mGraphic.rect_Gameplay[0]) && state == MC_UP)
		{
			switch_State(MC_IDLE);
			return true;
		} else if (m_Mc.collidesWith(mGraphic.rect_Gameplay[1]) && state == MC_DOWN)
		{
			switch_State(MC_IDLE);
			return true;
		}

		return false;
	}
	boolean sedangMengejarCinta;
	int indexCinta;
	boolean cintaYangDiKejarHilang;
	boolean sedangMemanah;
	Sprite target;
	
	void UpdateAI(Entity_Shinta shinta, float second)
	{
		if(shinta.listBenda.size() <= 0)
		{
			return;
		}
		
		if(shinta.listBenda.get(indexCinta).sprRandom!=shinta.listBenda.get(indexCinta).sprRacun){
		if(!(shinta.listBenda.get(indexCinta).sprRandom.getY() > m_Mc.getY()-Utils.getRatioH(95) && shinta.listBenda.get(indexCinta).sprRandom.getY() < m_Mc.getY()+Utils.getRatioH(40)))
		{
			if(shinta.listBenda.get(indexCinta).sprRandom.getY() > m_Mc.getY())
			{
				switch_State(MC_DOWN);
			}
			else
			{
				switch_State(MC_UP);
			}
		}
		else
		{
			if(panah.getPosX()+panah.rectPanah.getWidth() <= 0)
			{
				if(colItemChoco){
				if(isShoot){	
				delayTembak();
				}
				}
				else{
					if(isShoot){
				delaySuperSpeed();
					}
				}
			}
		}	
		}
	}
	
	boolean delayTembak(){
		if (!ButtonShootCooldownRahwana.getInstance().checkValid())
			return false;
			
		synchronized (this)
		{
			mSound.playSoundPanah();
			Utils.TRACE("attack");
			switch_State(MC_ATTACK);
			shoot();
		}
		return true;
	}
	
	boolean delaySuperSpeed(){
		if (!ButtonShootCooldownRahwana.getInstance().superSpeed())
			return false;
			
		synchronized (this)
		{
			mSound.playSoundPanah();
			Utils.TRACE("attack");
			switch_State(MC_ATTACK);
			shoot();
		}
		return true;
	}
	
	boolean AdaDalamRangeMenembak(Sprite sprite)
	{
		if(sprite.getY() < m_Mc.getY()+ Utils.getRatioH(80) && sprite.getY() > m_Mc.getY() - Utils.getRatioH(40))
		{
			return true;
		}
		return false;
	}
	
	void UpdatePanah(float pSecondElapsed, Entity_Shinta shinta, int id)
	{
		panah.onUpdate(pSecondElapsed, shinta, id);
	}
	
	boolean IsInScreen(Entity_Shinta shinta, int i)
	{
		
		if(shinta.listBenda.get(i).sprRandom.getY() < RES_HEIGHT && shinta.listBenda.get(i).sprRandom.getY() > 100)
		return true;
		
		return false;
		
		
	}
	
	public void shoot()
	{
//		listPanah.add(new Entity_Panah(m_Activity, m_Mc.getX(), m_Mc.getY(), mGraphic, m_Mc, false, Entity_Panah.PANAHNYA_RAHWANA));
		panah.rectPanah.setPosition(m_Mc.getX(), m_Mc.getY()+Utils.getRatioH(40));
		panah.sprPanah.setPosition(m_Mc.getX(), m_Mc.getY()+Utils.getRatioH(40));
		panah.panahApiAnim.setPosition(m_Mc.getX(), m_Mc.getY()+Utils.getRatioH(40));
		panah.rectPanah.getFirstChild();
		//listPanah.add(new Entity_Panah(m_Activity, m_Mc.getX(), m_Mc.getY(), mGraphic, m_Mc, false));
		Utils.TRACE("tembak....");
	}
}
